package com.yupi.springbootinit.controller;

import com.alibaba.fastjson.JSONObject;
import com.yupi.springbootinit.biz.TaskBiz;
import com.yupi.springbootinit.common.BaseResponse;
import com.yupi.springbootinit.common.OssDit;
import com.yupi.springbootinit.common.ResultUtils;
import com.yupi.springbootinit.exception.BusinessException;
import com.yupi.springbootinit.model.dto.TaskExcelModel;
import com.yupi.springbootinit.model.vo.TaskModel;
import com.yupi.springbootinit.model.vo.TaskReq;
import com.yupi.springbootinit.service.TaskService;
import com.yupi.springbootinit.utils.FileUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;

/**
 * 文件接口
 */
@RestController
@RequestMapping("/task")
@Slf4j
public class TaskController {

    @Resource
    private TaskBiz taskBiz;
    @Autowired
    private FileUtils fileUtils;
    @Autowired
    private TaskService taskService;


    /**
     * 查询任务集合
     */
    @PostMapping("/list")
    public BaseResponse<List<TaskModel>> list(@RequestBody TaskReq req) {
        log.info("list_req {}", JSONObject.toJSONString(req));
        List<TaskModel> stringListMap = taskBiz.queryList(req);
        return ResultUtils.success(stringListMap);
    }

    @PostMapping("/update")
    public BaseResponse<Boolean> update(@RequestBody TaskReq req) {
        log.info("update_req {}", JSONObject.toJSONString(req));
        taskBiz.update(req);
        return ResultUtils.success(true);
    }

    @PostMapping("/version")
    public BaseResponse<List<String>> getTaskVersion() {
        List<String> stringListMap = taskBiz.getTaskVersion();
        return ResultUtils.success(stringListMap);
    }

    @PostMapping("/uploadTask")
    public BaseResponse<Boolean> uploadTask(@RequestBody TaskReq req) {
        log.info("uploadTask req {}", JSONObject.toJSONString(req));
        String taskFileUrl = req.getTaskFileUrl();
        String filepath = taskFileUrl.replaceAll(OssDit.file_prefix_url, "");
        int lastDotIndex = filepath.lastIndexOf('.');
        String substring = filepath.substring(lastDotIndex + 1);
        if (!"xlsx".equals(substring)) {
            throw new BusinessException("文件格式错误");
        }
        ByteArrayOutputStream byteArrayOutputStream = fileUtils.downloadFileV2(taskFileUrl);
        InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        List<TaskExcelModel> stringListMap = FileUtils.readExcel(inputStream, TaskExcelModel.class);
        if (CollectionUtils.isEmpty(stringListMap)) {
            throw new BusinessException("文件为空");
        }
        taskService.saveTask(stringListMap);
        return ResultUtils.success(true);
    }


}
